<?php
defined('BASEPATH') OR exit('No direct script access allowed');
echo validation_errors(); 
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '统计分析管理/采购质量统计汇总',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">
    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
        $(".js-sidebar-nav>li:nth-child(11)>ul, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>ul").show(80);
        $(".js-sidebar-nav>li:nth-child(11)>a>span, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>a>span").addClass("sidebar-nav-sub-ico-rotate");
        $(".js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>a, .js-sidebar-nav>li:nth-child(11)>ul>li:nth-child(2)>ul>li:nth-child(1)>a").addClass("active");
    </script>
    
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper statement">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">按供应商类别统计</div>
                        </div>
                        <div class="widget-body  am-fr">
                            <div style="float: left;">
                                <div class="am-btn-toolbar">
                                    <div class="am-g">
                                        <div class="am-u-md-4 am-u-lg-4 am-form-group" style="width: 224px;">
                                            <button type="button" class="am-btn am-btn-default am-margin-right"
                                                    id="my-start">开始日期
                                            </button>
                                            <span id="my-startDate"></span>
                                        </div>
                                        <div class="am-u-md-4 am-u-lg-4 am-form-group"  style="width: 224px;">
                                            <button type="button" class="am-btn am-btn-default am-margin-right"
                                                    id="my-end">结束日期
                                            </button>
                                            <span id="my-endDate"></span>
                                        </div>
                                        <div class="am-u-md-4 am-u-lg-4 date-btn"  style="width: 224px;float: left;">
                                            <a href="javascript:;" onclick="getFiltersTime(30);">一个月</a>
                                            <a href="javascript:;" onclick="getFiltersTime(90);">一季度</a>
                                            <a href="javascript:;" onclick="getFiltersTime(365);">一年</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-u-md-2 am-u-lg-3" style="float: left;">
                                <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                    <input type="text" class="am-form-field" name="search" id="searchValue" placeholder="请输入供应商名称">
                                    <span class="am-input-group-btn">
                                        <a href="javascript:void(0)" id="search"
                                           class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                                    </span>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-form-group ">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs">
                                        <?php
                                        $type = isset($_GET['type']) ? $_GET['type'] : 'returnOrder';
                                        ?>
                                        <a href="javascript:void(0);" onclick="returnOrder()"
                                           class="am-btn   <?php if ($type == 'returnOrder') {
                                               echo 'am-btn-primary';
                                           } else {
                                               echo 'am-btn-default';
                                           } ?> ">退货统计</a>
                                        <a href="javascript:void(0);" onclick="barterOrder()"
                                           class="am-btn  <?php if ($type == 'barterOrder') {
                                               echo 'am-btn-primary ';
                                           } else {
                                               echo 'am-btn-default';
                                           } ?> ">换货统计</a>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="am-u-sm-12 am-form-group ">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs">
                                        <?php
                                            if($type == 'returnOrder'){
                                                echo ' <a href="/admin/statement/qualityBySupplierReturnDaoChu?type=returnOrder"  class="am-btn am-btn-primary"><span
                                                    class="am-icon-download"></span>退货导出</a>';
                                            }elseif($type == 'barterOrder'){
                                                echo ' <a href="/admin/statement/qualityBySupplierReturnDaoChu?type=barterOrder" onclick="" class="am-btn am-btn-primary"><span
                                                    class="am-icon-download"></span>换货导出</a>';
                                            }
                                        ?>

                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="am-u-sm-12 searchList">
                            <table width="100%" class="am-table am-table-compact table-hover tpl-table-black "
                                   id="example-r">
                                <thead>
                                <?php
                                if(isset($thlist) && $thlist){
                                    echo '<tr>
                                    <td>序号</td>
                                    <td>供应商编码</td>
                                    <td>供应商名称</td>
                                    <td>采购商品数量</td>
                                    <td>退货商品数量</td>
                                    <td>退货率</td>
                                    </tr>';
                                }else if(isset($hhlist) && $hhlist){
                                    echo '<tr>
                                    <td>序号</td>
                                    <td>供应商编码</td>
                                    <td>供应商名称</td>
                                    <td>采购商品数量</td>
                                    <td>换货商品数量</td>
                                    <td>换货率</td>
                                </tr>';
                                }
                                ?>
                                </thead>

                                <tbody class="choose-box">
		                            <?php
		                            if (isset($thlist) && $thlist) {
		                                foreach ($thlist as $k => $v) {                       		                           echo '<tr class="guige-ul">
		                                    		<td>' . $v['supplierId'] . '</td>
		                                    		<td><a href="/admin/statement/qualityBySupplierReturnDetails?supplierId='.$v['supplierId'].'">' . $v['code'] . '</a></td>
		                                    		<td>' . $k . '</td>
		                                    		<td>' . $v['purchaseNum'] . '</td>
		                                    		<td class="details-btn" data-am-modal="{target: \'#details\', closeViaDimmer: 0, width: 900, height: 500}">' . array_sum($v['quantity']) . '</td>
		                                    		<td>' . @round((array_sum($v['quantity']) / $v['purchaseNum']) * 100,2).'%' . '</td>
		                                    	  </tr>';
		                                }
		                            }else if(isset($hhlist) && $hhlist){
		                                foreach ($hhlist as $k2=>$v2) {
		                                    echo '<tr class="guige-ul">';
		                                    echo'<td>' . $v2['supplierId'] . '</td>';
		
		                                    echo '<td><a href="/admin/statement/qualityBySupplierBarterDetails?supplierId='.$v2['supplierId'].'">' . $v2['code'] . '</a></td>';
		                                    echo '<td>' . $k2 . '</td>
		                                        <td>' . $v2['purchaseNum'] . '</td>
		                                        <td class="details-btn" data-am-modal="{target: \'#details\', closeViaDimmer: 0, width: 900, height: 500}">' . array_sum($v2['quantity']) . '</td>
		                                        <td>' . round((array_sum($v2['quantity']) / $v2['purchaseNum']) * 100,2).'%' . '</td>';
		
		                                    echo    '</tr>';
		                                }
		                            }
		                            ?>
                            	</tbody>
                            </table>
                        </div>
                        <div class="am-u-lg-12">
                            <div class="am-u-sm-centered">
                                <ul class="am-pagination am-pagination-centered" id="paging">

                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 模态框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="details">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">详细订单信息
      <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
    </div>
    <div class="am-modal-bd">
    	<div class="am-u-md-12" >
    		<div class="am-u-md-1 am-u-lg-1" style="line-height: 32px;">
	            <label class="col-sm-centered am-md-text-right am-padding-left-0">筛选:</label>
	        </div>
	        <div class="am-u-md-5 am-u-lg-3">
	    		<div class="am-form-group tpl-table-list-select">
	                <select data-am-selected="{btnSize: 'sm',btnWidth: '100%'}" id="details-category" class="unlock">
	                    <option value="0">请选择商品分类</option>
	                    <option value="2">物料包</option>
	                    <option value="3">书籍</option>
	                    <option value="4">机器耗材</option>
	                    <option value="8">供应商商品</option>
	                    <option value="9">打印机租赁</option>
	                </select>
	            </div>
            </div>
            <div class="am-u-md-6 am-u-lg-4" style="float: left;">
                <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
                    <input type="text" class="am-form-field" placeholder="请输入商品编号搜索" name="search" id="details-searchValue">
                    <span class="am-input-group-btn">
                        <a href="javascript:;" id="details-search" class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                    </span>
                </div>
            </div>
    	</div>
    	<div class="am-u-md-12 am-scrollable-vertical" style="height: 332px;">
	    	<table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " style="margin-bottom:0;" id="details-table">
	            <thead>
		            <tr>
		                <th style="width: 120px">序号</th>
		                <th style="width: 120px">商品编码</th>
		                <th style="width: 120px">商品名称</th>
		                <th style="width: 120px">商品分类</th>
		                <th style="width: 100px">供应商名称</th>
		                <?php
	                        if (isset($thlist) && $thlist) {
	                    echo '<th style="width: 100px">退货时间</th>';
		                echo '<th style="width: 100px">退货数量</th>';
	                       }else if(isset($hhlist) && $hhlist){
		                echo '<th style="width: 100px">换货时间</th>';
		                echo '<th style="width: 100px">换货数量</th>';
	                        }
	                     ?>
		            </tr>
	            </thead>
	            <tbody>
	            </tbody>
	        </table>
    	</div>
    	<div class="am-u-md-12">
    		<div class="am-u-sm-centered">
    			<ul class="am-pagination am-pagination-centered" id="details-paging">
    			</ul>
    		</div>
    	</div>
    </div>
</div>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>

<script>
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)return unescape(r[2]);
        return null;
    }
    function barterOrder() {
        var search = GetQueryString('search');
        // $(".am-btn-primary").removeClass("am-btn-primary");
        // $(this).addClass("am-btn-primary");

        if (search == null) {
            window.location.href = '?page=1' + '&type=barterOrder'
        } else {
            window.location.href = '?page=1' + '&search=' + search + '&type=barterOrder'
        }
    }
    function returnOrder() {
        var search = GetQueryString('search');
        if (search == null) {
            window.location.href = '?page=1' + '&type=returnOrder'
        } else {
            window.location.href = '?page=1' + '&search=' + search + '&type=returnOrder'

        }
    }

</script>

<script>

    //类型传参 type == 类型， search == 要搜索的
    $("#search").on("click",function () {
        var search = $("#searchValue").val();
        var type = "<?php echo isset($_GET['type']) ? $_GET['type'] : 'returnOrder' ?>";
        var page = "<?php echo isset($_GET['page']) ? $_GET['page'] : 1 ?>";
        var startTime = $("#my-startDate").text();
        var endTime = $("#my-endDate").text();
        chooseCheck(type,search,page,startTime,endTime);
    });

    //筛选日期
    var startDate = new Date(2014, 11, 20);
    var endDate = new Date(2100, 11, 25);
    var $alert = $('#data-alert');
    $('#my-start').datepicker().on('changeDate.datepicker.amui', function(event) {

        if (event.date.valueOf() > endDate.valueOf()) {
            $alert.find('p').text('开始日期应小于结束日期！').end().show();
            return false;

        } else {
            $alert.hide();
            startDate = new Date(event.date);
            $('#my-startDate').text($('#my-start').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        search = $("#searchValue").val();
        type = "<?php echo isset($_GET['type']) ? $_GET['type'] : 'returnOrder' ?>";
        page = "<?php echo isset($_GET['page']) ? $_GET['page'] : 1 ?>";

        chooseCheck(type,search,page,startTime,endTime);

    });
    $('#my-end').datepicker().on('changeDate.datepicker.amui', function(event) {

        if (event.date.valueOf() < startDate.valueOf()) {
            $alert.find('p').text('结束日期应大于开始日期！').end().show();
            return false;
        } else {
            $alert.hide();
            endDate = new Date(event.date);
            $('#my-endDate').text($('#my-end').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        search = $("#searchValue").val();
        type = "<?php echo isset($_GET['type']) ? $_GET['type'] : 'returnOrder' ?>";
        page = "<?php echo isset($_GET['page']) ? $_GET['page'] : 1 ?>";


        chooseCheck(type,search,page,startTime,endTime);

    });
    
    var supplierId,
    	url = '';
    	
    $(".details-btn").on("click",function () {
    	var one_category = '';
    	var product_no = ''; 
    	supplierId = $(this).parent().find("td:first-child").text();
    	supplierId = supplierId - 0;
    	var type = GetQueryString('type');
    	if (!type || type == 'returnOrder') {
    		url = "/api/statement/returnContent";
    	} else if (type == 'barterOrder') {
    		url = "/api/statement/barterContent";
    	}
    	$("#details-category").val("0");
    	$("#details-searchValue").val("");
    	$("select").trigger('changed.selected.amui');
    	detailsCheck(url,'','',supplierId,1);
    });
    
    $("#details-search").on("click", function () {
    	var one_category = $("#details-category").val() == '0' ? '' : $("#details-category").val() ;
    	var product_no = $("#details-searchValue").val();
    	detailsCheck(url,one_category,product_no,supplierId,1);
    });
    
    function detailsCheck(url,one_category,product_no,supplierId,page) {
    	$.ajax({
    		type:"get",
    		url: url,
    		data: {
    			one_category: one_category,
    			product_no: product_no,
    			supplierId: supplierId,
    			page: page
    		},
    		dateType : "json",
    		success: function (msg) {
    			var msg = $.parseJSON(msg);
    			console.log(msg)
    			var html = '';
    			var data = msg.pageData;
    			for ( var i = 0; i < data.length; i ++ ) {
    				html += '<tr>';
	    			html += 	'<td>'+ data[i].dataId +'</td>';
	    			html += 	'<td>'+ data[i].product_no +'</td>';
	    			html += 	'<td>'+ data[i].name +'</td>';
	    			html += 	'<td>'+ data[i].one_category +'</td>'; 
	    			html += 	'<td>'+ data[i].supplier +'</td>';
	    			html += 	'<td>'+ changePhpTime(data[i].createTime) +'</td>';
	    			html += 	'<td>'+ data[i].quantity +'</td>';
    				html += '</tr>';
    			}
    			$("#details-table").find("tbody").html(html);
    			
      			var pageOptions = {
                      AllowPaging: true,
                      PageIndex: msg.currentPage,      //设置当前页码
                      PageSize: msg.pageNum,       //设置分页大小
                      RecordCount: msg.totalNum,  //设置数据总数
                      // TotalPage: ,      //设置总页数
                      showPageCount: 4,
                      onPageClick: function(pageIndex) {
                          detailsCheck(url,one_category,product_no,supplierId,pageIndex);
                      }
                  };
                  //初始化分页栏
                  $('#details-paging').pageBar(pageOptions);
    		},
    		error : function (data) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
    	});
    }
    
    function changePhpTime (time) {
    	var y,m,d,date;
    	date = new Date(time*1000);
    	y = date.getFullYear() + '-';
    	m = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    	d = date.getDate();
    	return y + m + d;
    }

    function chooseCheck(type,search,page,startTime,endTime) {
        $.ajax({
            type: "GET",
            url : "/api/statement/qualityBySupplierSearch",
            data : {
                type : type,
                search : search,
                page: page,
                startTime:startTime,
                endTime: endTime
            },
            dateType : "json",
            success : function (msg) {
                var data = JSON.parse(msg);
                var list = '';

                if(data.error ==  true){
                    $('.searchList').nextAll().remove();


                    $(".searchList").html(data.info);
                    return false;
                }

                var sum = 0;

                // 计算数组的总和
                function arraySum(item) {
                    var nummber = null;
                    for(var i=0;i<item.length;i++){
                        nummber += item[i] - 0;
                    }
                    return nummber;
                }

                switch (data.type){
                    case 'returnOrder':
                        list += ' <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " id="example-r"><thead>'
                            + '<tr>'
                            +' <th>序号</th> '
                            + '<th>供应商编码</th>'
                            + '<th>供应商名称</th>'
                            + '<th>采购商品数量</th>'
                            + '<th>退货商品数量</th>'
                            + '<th>退货率</th>'
                            + '</tr>'
                            + '</thead>'
                            + '<tbody class="choose-box">';
                        for(var key in data[0]){
                            var quantity = data[0][key].quantity;
                            list += "<tr>"
                                +  '<th>'+ data[0][key].supplierId +'</th>'
                                +  '<td><a href="/admin/statement/qualityBySupplierReturnDetails?supplierId= '+ data[0][key].supplierId +' ">'+ data[0][key].code +'</a></td>'
                                +  '<th>'+ key +'</th>'
                                +  '<th>'+ data[0][key].purchaseNum +'</th>'
                                +  '<th>'+ arraySum(quantity) +'</th>'
                                +  '<th>'+ ((arraySum(quantity) / data[0][key].purchaseNum)*100).toFixed(2)+'%' +'</th>'
                                + '</tr>';
                        }

                        list += '</tbody>'
                            + '</table>';
                        break;

                    case 'barterOrder':
                        list += ' <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " id="example-r"><thead>'
                            + '<tr>'
                            +' <th>序号</th> '
                            + '<th>供应商编码</th>'
                            + '<th>供应商名称</th>'
                            + '<th>采购商品数量</th>'
                            + '<th>换货商品数量</th>'
                            + '<th>换货率</th>'
                            + '</tr>'
                            + '</thead>'
                            + '<tbody class="choose-box">';
                        for(var key2 in data[0]){
                            var quantity2 = data[0][key2].quantity;
                            list += "<tr class='guige-ul'>"
                                +  '<td>'+ data[0][key2].supplierId +'</td>'
                                +  '<td><a href="/admin/statement/qualityBySupplierReturnDetails?supplierId= '+ data[0][key2].supplierId +' ">'+ data[0][key2].code +'</a></td>'
                                +  '<td>'+ key2 +'</td>'
                                +  '<td>'+ data[0][key2].purchaseNum +'</td>'
                                +  '<td>'+ arraySum(quantity2) +'</td>'

                                +  '<td>'+ ((arraySum(quantity2) / data[0][key2].purchaseNum)*100).toFixed(2)+'%' +'</td>'

                                + '</tr>';
                        }

                        list += '</tbody>'
                            + '</table>';
                        break;
                }


                //查询到结果之后
                $(".searchList").html(list);

                //分页
                //设置分页信息
                console.log(data.total_num);
//                var pageOptions = {
//                    AllowPaging: true,
//                    PageIndex: data.this_page,      //设置当前页码
//                    PageSize: data.pageNum,       //设置分页大小
//                    RecordCount: data.total_num,  //设置数据总数
//                    // TotalPage: ,      //设置总页数
//                    showPageCount: 4,
//                    onPageClick: function(pageIndex) {
//                        chooseCheck(type,search,pageIndex,startTime,endTime);
//                    }
//                };
//                //初始化分页栏
//                $('#paging').pageBar(pageOptions);


            },
            error : function (data) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        })

    }





</script>

<script>
//    $(document).ready(function () {
//        //设置分页信息
//        var type = "<?php //echo isset($_GET['type']) ? $_GET['type'] : 'returnOrder' ?>//";
//        var pageOptions = {
//            AllowPaging: true,
//            PageIndex: <?php //echo $_GET['page'] ?>//,      //设置当前页码
//            PageSize: <?php //echo $pageNum ?>//,       //设置分页大小
//            RecordCount: <?php //echo $totalNum ?>//,  //设置数据总数
//            // TotalPage: ,      //设置总页数
//            showPageCount: 4,
//            onPageClick: function (pageIndex) {
//                var page = parseInt(pageIndex);
//                window.location.href = "?page=" + page + "&type=" + type;
//            }
//        };
//        //初始化分页栏
//        $('#paging').pageBar(pageOptions);
//    })
</script>
</body>

</html>